﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using System.Xml;

namespace HZ.DrugTrade.Web.Controls
{
    public partial class Menu : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindTreeView(TreeView2);
            }
           
        }


        #region  绑定TreeView控件
        /// <summary>
        /// 绑定TreeView控件
        /// </summary>
        /// <param name="treeview">TreeView控件</param> 
        protected void BindTreeView(TreeView treeview)
        {
            //利用xmldoc对象读取xml文件
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(HttpContext.Current.Request.PhysicalApplicationPath + "/Upload/PermissionDefault.xml");
            //读取根节点
            XmlElement xmlele = xmldoc.DocumentElement;
            TreeNode tn = new TreeNode();            
            tn.Text = "管理菜单";
            tn.Value = "";
            //获取根节点下的所有节点
            XmlNodeList xmlnl = xmlele.ChildNodes;
            //递归遍历节点 
            TreeNode tn_nodes = null;
            foreach (XmlNode xmlnode in xmlnl)
            {
                if (xmlnode.NodeType != XmlNodeType.Comment)
                {
                    tn_nodes = new TreeNode();
                    tn_nodes.Text = xmlnode.Attributes["title"].Value;
                    tn_nodes.Value = xmlnode.Attributes["description"].Value;
                    tn_nodes.NavigateUrl = xmlnode.Attributes["url"].Value;

                    if (xmlnode.Attributes["status"].Value == "1")
                    {
                        tn_nodes.Checked = true;
                    }
                    GetNodes(xmlnode, tn_nodes);
                    tn.ChildNodes.Add(tn_nodes);
                }
            }
            treeview.Nodes.Add(tn);
        }

        #endregion

        #region 递归遍历节点
        /// <summary>
        /// //递归遍历节点
        /// </summary>
        /// <param name="xmlnode">当前xml文件中的节点</param>
        /// <param name="tn">treeview中当前节点</param>
        public void GetNodes(XmlNode xmlnd, TreeNode tn)
        {
            //获取根节点下的所有节点
            XmlNodeList xmlnl = xmlnd.ChildNodes;
            TreeNode tn_nodes = null;
            foreach (XmlNode xmlnode in xmlnl)
            {
                if (xmlnode.NodeType != XmlNodeType.Comment)
                {
                    if (xmlnode.ChildNodes.Count > 0)
                    {
                        tn_nodes = new TreeNode();
                        tn_nodes.Text = xmlnode.Attributes["title"].Value;
                        tn_nodes.Value = xmlnode.Attributes["description"].Value;
                        if (xmlnode.Attributes["target"] != null)
                        {
                            tn_nodes.Target = xmlnode.Attributes["target"].Value;
                        }
                        tn_nodes.NavigateUrl = xmlnode.Attributes["url"].Value;
                        //if (xmlnode.Attributes["status"].Value == "1")
                        //{
                        //    tn_nodes.Checked = true;
                        //}
                        GetNodes(xmlnode, tn_nodes);
                        tn.ChildNodes.Add(tn_nodes);
                    }
                    else
                    {
                        tn_nodes = new TreeNode();
                        tn_nodes.Text = xmlnode.Attributes["title"].Value;
                        tn_nodes.Value = xmlnode.Attributes["description"].Value;
                        tn_nodes.NavigateUrl = xmlnode.Attributes["url"].Value;
                        if (xmlnode.Attributes["target"] != null)
                        {
                            tn_nodes.Target = xmlnode.Attributes["target"].Value;
                        }
                        //if (xmlnode.Attributes["status"].Value == "1")
                        //{
                        //    tn_nodes.Checked = true;
                        //}
                        tn.ChildNodes.Add(tn_nodes);
                    }
                }
            }
        }
        #endregion
    }
}